package ControlLayer;
import java.util.*;
import ModelLayer.*;

/**
 * The LoanCtr make the connection between the ModemLayer and the TUI;
 * 
 * @author (Grup2) 
 * @version (2)
 */
public class LoanCtr
{    
    private ControlDVD controldvd = ControlDVD.getInstance();
    private AddressCtr addressctr = AddressCtr.getInstance();
    private LoanCollection loancollectioninst=LoanCollection.getInstance();

    public LoanCtr()
    {

    }
    //check if a person it's in the system and if there are copy available
    public boolean checkIfAvailable(String DVDTitle, String personId)
    {
        boolean available=false;
        if(controldvd.checkCopiesOfDVD(DVDTitle)==true)
        {
            if(addressctr.checkPersonId(personId)==true)
            {
                available=true;
            }
        }
        return available;
    }
    
    // create a loan
    public void createLoan(String DVDTitle,String personId, String borrowDate, String period, String personid, String loanId, String copyId, int serialNumber)
    {
        if(checkIfAvailable(DVDTitle, personId)==true)
        {
            Loan loan = new Loan(loanId, borrowDate, period, personid, copyId);
            if(controldvd.getCopyStatus(serialNumber).equals("available"))
            {
                loancollectioninst.addLoan(loan);
                String status="not available";
                controldvd.modifyCopyStatus(serialNumber, status);
                System.out.println("Loan created!");
            }
            else
            {
                System.out.println("Error!");
            }
        }
        else
        {
            System.out.println("Error!");
        }
    }
    // start calling methods from the ModemLayer 
    public void readLoan(String personId)
    {
        loancollectioninst.readLoan(personId); 
    }

    public void deleteLoan(String personId)
    {
        loancollectioninst.deleteLoan(personId);
    }

    public void returnLoan(String loanId, int serialNumber)
    {
        loancollectioninst.returnLoan(loanId);
        String status="available";
        controldvd.modifyCopyStatus(serialNumber, status);
        System.out.println("Succeed");
    }
    
    public void updateLoan(String personId, String borrowDate, String period, String copyID)
    {
        loancollectioninst.updateLoan(personId, borrowDate, period, copyID);
    }
    // end calling methods from the ModemLayer 
}
